package com.sc.web.gwt.client.view.uicomponent;

/**
 * User: KienCT
 * Date: 22/07/2011
 * Time: 10:03
 */

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;

/**
 * A dialog that provides a Yes/No option.
 */
public class ConfirmDialog extends Dialog {
    private final Label question;
    private final Label info;

    private ClickHandler yesHandler;

    public ConfirmDialog() {
        DialogMessages messages = GWT.create(DialogMessages.class);
        question = new Label();
        info = new Label();

        RESOURCES.dialogCSS().ensureInjected();
        question.addStyleName(RESOURCES.dialogCSS().questionLabel());
        info.addStyleName(RESOURCES.dialogCSS().infoLabel());

        setBody(question, info);

        Button yesButton = new Button(messages.yes());
        yesButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                yesHandler.onClick(event);
            }
        });

        Button noButton = new Button(messages.no());
        noButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                hide();
            }
        });

        addButton(yesButton);
        addButton(noButton);
    }

    /**
     * show a confirmation dialog (Yes/No), with the given yes handlers.
     */
    public void show(String title, String questionMsg, String infoMsg, ClickHandler yesHandler) {
        this.yesHandler = yesHandler;

        setTitle(title);
        question.setText(questionMsg);
        info.setText(infoMsg);

        center();
        show();
    }

}
